package cn.scw.project.goods.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.goods.domain.GCart;
import cn.scw.project.goods.domain.GCommodity;
import cn.scw.project.goods.domain.netework.bean.GCartAddReq;
import cn.scw.project.goods.domain.vo.GCartVo;
import cn.scw.project.goods.mapper.GCartMapper;
import cn.scw.project.goods.service.IGCartService;
import cn.scw.project.goods.service.IGCommodityModelService;
import cn.scw.project.goods.service.IGCommodityService;
import cn.scw.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GCartServiceImpl implements IGCartService {
    @Autowired
    private GCartMapper gCartMapper;
    @Autowired
    private IGCommodityService igCommodityService;
    @Autowired
    private IGCommodityModelService igCommodityModelService;

    /**
     * 查询购物车
     *
     * @param id 购物车ID
     * @return 购物车
     */
    @Override
    public GCart selectGCartById(Long id) {
        return gCartMapper.selectGCartById(id);
    }

    /**
     * 查询购物车列表
     *
     * @return 购物车
     */
    @Override
    public List<GCartVo> selectGCartList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        return gCartMapper.selectGCartList(user.getUserId());
    }

    /**
     * 新增购物车
     *
     * @param req 购物车
     * @return 结果
     */
    @Override
    public int insertGCart(GCartAddReq req) {
        GCommodity gCommodity = igCommodityService.selectGCommodityById(req.getCommodityId());
        if (gCommodity == null)
            throw new CustomException("商品不存在");
        if (gCommodity.getIsModel()) {
            if (req.getModelId() == null)
                throw new CustomException("请选择商品规格");
            if (igCommodityModelService.selectGCommodityModelById(req.getModelId()) == null)
                throw new CustomException("商品规格不存在");
        }
        if (req.getCount() < 1)
            throw new CustomException("请选择正确的商品数量");
        SysUser user = SecurityUtils.getLoginUser().getUser();

        GCart gCart = this.selectGCartByCommodityIdAndModelId(user.getUserId(), req.getCommodityId(), req.getModelId());
        if (gCart == null) {
            gCart = new GCart();
            ToolUtils.copyPropertiesIgnoreNull(req, gCart);
            gCart.setUserId(user.getUserId());
        } else {
            gCart.setCount(gCart.getCount() + req.getCount());
        }

        return gCartMapper.insertGCart(gCart);
    }

    /**
     * 修改购物车
     *
     * @param req 购物车
     * @return 结果
     */
    @Override
    public int updateGCart(GCartAddReq req) {
        GCart gCart = this.selectGCartById(req.getId());
        if (gCart==null)
            throw new CustomException("商品不存在");
        if (req.getCount()<1)
            throw new CustomException("商品数量不能小于1");
        gCart.setCount(req.getCount());
        return gCartMapper.updateGCart(gCart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车ID
     * @return 结果
     */
    @Override
    public int deleteGCartByIds(Long[] ids) {
        return gCartMapper.deleteGCartByIds(ids);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车ID
     * @return 结果
     */
    @Override
    public int deleteGCartById(Long id) {
        return gCartMapper.deleteGCartById(id);
    }

    @Override
    public GCart selectGCartByCommodityIdAndModelId(Long userId, Long commodityId, Long modelId) {
        return gCartMapper.selectGCartByCommodityIdAndModelId(userId, commodityId, modelId);
    }
}
